<template>

</template>

<script setup name="Patient">
import { listPatient, getPatient, delPatient, addPatient, updatePatient } from "@/api/sn/patient";

const { proxy } = getCurrentInstance();

const patientList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    realName: null,
    membershipLevelId: null,
    medicalRecordId: null,
    phoneNumber: null,
    age: null,
    gender: null,
    idCardNumber: null,
    address: null,
    ethnicId: null,
    occupationId: null,
    physicalInfoId: null,
    prescriptionInfoId: null,
    isHospitalized: null,
    bedId: null,
    receptionStatus: null,
    ext1: null,
    ext2: null,
    ext3: null,
    ext4: null,
    ext5: null
  },
  rules: {
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询患者接诊列表 */
function getList() {
  loading.value = true;
  listPatient(queryParams.value).then(response => {
    patientList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    patientId: null,
    realName: null,
    membershipLevelId: null,
    medicalRecordId: null,
    phoneNumber: null,
    age: null,
    gender: null,
    idCardNumber: null,
    address: null,
    ethnicId: null,
    occupationId: null,
    physicalInfoId: null,
    prescriptionInfoId: null,
    isHospitalized: null,
    bedId: null,
    receptionStatus: null,
    ext1: null,
    ext2: null,
    ext3: null,
    ext4: null,
    ext5: null
  };
  proxy.resetForm("patientRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.patientId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加患者接诊";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _patientId = row.patientId || ids.value
  getPatient(_patientId).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改患者接诊";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["patientRef"].validate(valid => {
    if (valid) {
      if (form.value.patientId != null) {
        updatePatient(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addPatient(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _patientIds = row.patientId || ids.value;
  proxy.$modal.confirm('是否确认删除患者接诊编号为"' + _patientIds + '"的数据项？').then(function() {
    return delPatient(_patientIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}


getList();
</script>
